<?php namespace shared\model\user;
/**
 * @subpackage  \shared\model\user
 *
 * @copyright   Copyright (C) 2013 - 2014 Mailerm, Inc. All rights reserved.
 */

# Load Libraries
require_once MPATH_LIBRARIES . '/database/MySQL.php';

use \MySQL as MySQL;

# Load Configuration
require_once MPATH_MODULES . '/configuration.php';

// Save user into database
// @return bool
function save($project, $account, $password, $role, $status, $create_time, $update_time, $login_time, $login_ip) {
	# Connect Database
    $database = new MySQL(MYSQL_DB_HOST, MYSQL_DB_USER, MYSQL_DB_PASS, MYSQL_DB_NAME, MYSQL_DB_CHARSET);   
    # Insert All
    $database->prepare('INSERT INTO `' . TBL_USER . '` (`id`, `project`, `account`, `password`, `role`, `status`, `create_time`, `update_time`, `login_time`, `login_ip`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
    $database->execute(null, $project, $account, $password, $role, $status, $create_time, $update_time, $login_time, $login_ip);
    return $database->insert_id() < 0 ? false : true;
}

// Execute sql
// @return bool
function execute($sql) {
    # Connect Database
    $database = new MySQL(MYSQL_DB_HOST, MYSQL_DB_USER, MYSQL_DB_PASS, MYSQL_DB_NAME, MYSQL_DB_CHARSET);
    # execute
    $database->prepare($sql);
    $database->execute();
    return $database;
}

// Count users
// @return count
function count($project) {
	# Connect Database
    $database = new MySQL(MYSQL_DB_HOST, MYSQL_DB_USER, MYSQL_DB_PASS, MYSQL_DB_NAME, MYSQL_DB_CHARSET);
    # Count All
    $database->prepare('SELECT COUNT(`id`) AS \'count\' FROM `' . TBL_USER . '` WHERE `status`!=? AND `project`=?');
    $database->execute(USER_STATUS_DELETED, $project);
    $results = $database->results();    
    # Num Rows
    if($database->num_rows() < 1) {
        return -1;
    }
    return intval($results[0]['count']);
}

// Find available users
// @return array
function find($project, $start, $size) {
	# Connect Database
    $database = new MySQL(MYSQL_DB_HOST, MYSQL_DB_USER, MYSQL_DB_PASS, MYSQL_DB_NAME, MYSQL_DB_CHARSET);
	$database->prepare('SELECT `id`,`account`,`status`,`role`,`login_time`,`login_ip` FROM `' . TBL_USER . '` WHERE `status`!=? AND `project`=? LIMIT ?,?');
    $database->execute(USER_STATUS_DELETED, $project, $start, $size);
    $users = $database->results();
    return $users;
}

// Get user by id
// @return array
function get($id) {
	# Connect Database
    $database = new MySQL(MYSQL_DB_HOST, MYSQL_DB_USER, MYSQL_DB_PASS, MYSQL_DB_NAME, MYSQL_DB_CHARSET);
    $database->prepare('SELECT `id`,`project`,`account`,`password`,`role`,`login_time`,`login_ip`,`status` FROM `' . TBL_USER . '` WHERE `id`=? LIMIT 1');
    $database->execute($id);
    $results = $database->results();
    # Num Rows
    if($database->num_rows() < 1){
        return null;
    }
    return $results[0];
}

// Get user by project and role
// @return array
function get_by_project($project, $role) {
    # Connect Database
    $database = new MySQL(MYSQL_DB_HOST, MYSQL_DB_USER, MYSQL_DB_PASS, MYSQL_DB_NAME, MYSQL_DB_CHARSET);
    $database->prepare('SELECT `id`,`project`,`account`,`password`,`role`,`login_time`,`login_ip`,`status` FROM `' . TBL_USER . '` WHERE `project`=? AND `role`=?');
    $database->execute($project, $role);
    $results = $database->results();
    # Num Rows
    if($database->num_rows() < 1) {
        return array();
    }
    return $results;
}

// Get user by account
// @return array
function get_by_account($account) {
	# Connect Database
	$database = new MySQL(MYSQL_DB_HOST, MYSQL_DB_USER, MYSQL_DB_PASS, MYSQL_DB_NAME, MYSQL_DB_CHARSET);
    $database->prepare('SELECT `id`,`project`,`account`,`password`,`role`,`login_time`,`login_ip`,`status` FROM `' . TBL_USER . '` WHERE `account`=? AND `status`!=? LIMIT 1');
    $database->execute($account, USER_STATUS_DELETED);
    $results = $database->results();
    # Num Rows
    if($database->num_rows() < 1) {
        return null;
    }
    return $results[0];
}

// Active users
// @return affected
function active($ids, $except) {
	$tmp = '0';
    foreach ($ids as $key => $value) {
        $tmp = $tmp . ',' . $value;
    }
	# Connect Database
    $database = new MySQL(MYSQL_DB_HOST, MYSQL_DB_USER, MYSQL_DB_PASS, MYSQL_DB_NAME, MYSQL_DB_CHARSET);
    $database->prepare('UPDATE `' . TBL_USER . '` SET `status`=? WHERE `status`=? AND `id`!=? AND `id` IN (' . $tmp . ')');
    $database->execute(USER_STATUS_NORMAL, USER_STATUS_BANNED, $except);
    return $database->affected_rows();
}

// Ban users
// @return affected
function ban($ids, $except) {
	$tmp = '0';
    foreach ($ids as $key => $value) {
        $tmp = $tmp . ',' . $value;
    }
	# Connect Database
    $database = new MySQL(MYSQL_DB_HOST, MYSQL_DB_USER, MYSQL_DB_PASS, MYSQL_DB_NAME, MYSQL_DB_CHARSET);
    $database->prepare('UPDATE `' . TBL_USER . '` SET `status`=? WHERE `status`=? AND `id`!=? AND `id` IN (' . $tmp . ')');
    $database->execute(USER_STATUS_BANNED, USER_STATUS_NORMAL, $except);
    return $database->affected_rows();
}

// Delete users
// @return affected
function delete($ids, $except) {
	$tmp = '0';
    foreach ($ids as $key => $value) {
        $tmp = $tmp . ',' . $value;
    }
	# Connect Database
    $database = new MySQL(MYSQL_DB_HOST, MYSQL_DB_USER, MYSQL_DB_PASS, MYSQL_DB_NAME, MYSQL_DB_CHARSET);
    $database->prepare('UPDATE `' . TBL_USER . '` SET `status`=? WHERE `status`!=? AND `id`!=? AND `id` IN (' . $tmp . ')');
    $database->execute(USER_STATUS_DELETED, USER_STATUS_DELETED, $except);
    return $database->affected_rows();
}